/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "ESummary.h"

#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <QtCore/QFile>

#include "OutputParser.h"

using namespace Bio;

const QString	c_esummaryUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi";

///////////////////////////////////////////////////////////////////////////////
//	ESummary
///////////////////////////////////////////////////////////////////////////////

/**
	@class ESummary
	@brief Implementation of the Entrez 'ESummary' function.

	Provides an interface to the NCBI Entrez 'ESummary' function.

	See http://www.ncbi.nlm.nih.gov/books/NBK25499/ for details on
	the Entrez ESummary utility.
**/

/**
	Default Constructor. Creates a new ESummary operation.
**/
ESummary::ESummary()
{
}

/**
**/
ESummary::ESummary( const QString &db, const QString &id ) : m_db( db ), m_queryKey(-1 ),
	m_retStart( -1 ), m_retMax( -1 )
{
	m_ids.append( id );
}

/**
**/
ESummary::ESummary( const QString &db, const QStringList &ids ) : m_db( db ), m_ids( ids ),
	m_queryKey(-1 ), m_retStart( -1 ), m_retMax( -1 )
{
}

/**
**/
ESummary::ESummary( const QString &db, int queryKey, const QString &webEnv ) : m_db( db ),
	m_queryKey( queryKey ), m_webEnv( webEnv ), m_retStart( -1 ), m_retMax( -1 )
{
}

/**
**/
ESummary::~ESummary()
{
}

/**
**/
bool ESummary::exec()
{
	QUrl			url( c_esummaryUrl );
	QUrlQuery		urlQuery;
	QFile			output( "ESummary.txt" );
	OutputParser	parser( output );

	if( m_db.isEmpty() ){
		error( "No database specified for ESummary." );
		return false;
	}

	urlQuery.addQueryItem( "db", m_db );

	if( m_retStart >= 0 )
		urlQuery.addQueryItem( "retstart", QString::number( m_retStart ) );

	if( m_retMax >= 0 )
		urlQuery.addQueryItem( "retmax", QString::number( m_retMax ) );

	// url.addQueryItem( "version", "2.0" );

	if( m_queryKey < 0 ){
		if( m_ids.isEmpty() ){
			error( "Must specify IDs or history settings for ESummary." );
			return false;
		}

		if( m_ids.count() > 200 )
			return execPost( url, m_ids, parser );
		
		urlQuery.addQueryItem( "id", m_ids.join( "," ) );
	}
	else {
		if( m_webEnv.isEmpty() ){
			error( "Invalid WebEnv specified for ESummary." );
			return false;
		}

		urlQuery.addQueryItem( "query_key", QString::number( m_queryKey ) );
		urlQuery.addQueryItem( "WebEnv", m_webEnv );
	}

	url.setQuery( urlQuery );

	return execGet( url, parser );
}

/**
**/
void ESummary::setDatabase( const QString &db )
{
	m_db = db;
}

/**
**/
QString ESummary::getDatabase() const
{
	return m_db;
}

/**
**/
void ESummary::addID( const QString &id )
{
	m_ids.append( id );
}

/**
**/
void ESummary::setIDs( const QStringList &ids )
{
	m_ids = ids;
}

/**
**/
QStringList ESummary::getIDs() const
{
	return m_ids;
}

/**
**/
void ESummary::setHistory( int queryKey, const QString &webEnv )
{
	m_queryKey = queryKey;
	m_webEnv = webEnv;
}

/**
**/
int ESummary::getQueryKey() const
{
	return m_queryKey;
}

/**
**/
QString ESummary::getWebEnv() const
{
	return m_webEnv;
}

/**
**/
void ESummary::setRetStart( int num )
{
	m_retStart = num;
}

/**
**/
int ESummary::getRetStart() const
{
	return m_retStart;
}

/**
**/
void ESummary::setRetMax( int num )
{
	m_retMax = num;
}

/**
**/
int ESummary::getRetMax() const
{
	return m_retMax;
}
